/*********************************************************************************
Organization 					: 				Georgia Institute of Technology
  
Institute 						:				Cognitive Computing Group(CCL)
 
Authors							: 				Santiago Ontanon, Kinshuk Mishra
 												Neha Sugandh 
 												
Class							:				BuildingCompleteCondition
 
Function						: 				Provides a class for holding 
												a building complete condition
****************************************************************************/
package planninglanguage.conditions;

import org.jdom.Element;

import util.XMLWriter;
import base.WargusGameState;
import base.WargusPlayer;
import base.WargusStateImporter;
import base.WargusUnit;


public class BuildingCompleteCondition extends Condition {

	int m_playerID;
	int m_unitID;
	String m_unitType;
	int m_x;
	int m_y;
	
	public BuildingCompleteCondition(String a_buildingType,int a_x,int a_y,int a_playerID)
	{
		m_playerID = a_playerID;
		m_unitID = -1;
		m_unitType = a_buildingType;
		m_x = a_x;
		m_y = a_y;
	}	

	public BuildingCompleteCondition(int unitID)
	{
		m_playerID = -1;
		m_unitID = unitID;
		m_unitType = null;
		m_x = -1;
		m_y = -1;
	}	
	public String toString(int tabs) {
		String n = super.toString(tabs);
		n = n+ "Player Id is:" + m_playerID;
		n = n + "Unit Id is" + m_unitID;
		n = n + "Unit Type is" + m_unitType;
		n = n + "X coordinate is" + m_x;
		n = n + "Y coordinate is" + m_y;
		
		return n;
	}	
	public boolean compare(BuildingCompleteCondition c)
	{
		if(m_playerID==c.m_playerID && m_unitType.equals(c.m_unitType))
			return true;
		else
			return false;
	}
	
	public String getUnitType()
	{
		return m_unitType;
	}
	public void setUnitType(String t) {
		m_unitType = t;
	}
	
	public boolean test(WargusGameState a_gameState)
	{
		WargusPlayer p = a_gameState.getPlayer(m_playerID);
		
//		System.out.println("BuildingCompleteCondition: test(player = " + m_playerID + " , unitType = " + m_unitType + " X = " + m_x + " , Y = " + m_y);
		
		if (p!=null) {
			for(WargusUnit unit:p.getUnits()) {
	
				if (m_unitType == null) {
					if (unit.getUnitID() == m_unitID &&
						unit.getHitPoints() == WargusStateImporter.unitMaxHitPoints(unit.getType())) return true; 
				} else {
					if (WargusStateImporter.unitTypeToString(unit.getType()).equals(m_unitType) &&
						unit.getLocX() == m_x &&
						unit.getLocY() == m_y &&
						unit.getHitPoints() == WargusStateImporter.unitMaxHitPoints(unit.getType())) return true;
				}
			}
			
		}
		return false;
	}
	
	public void writeXML(XMLWriter w) {
		w.tag("condition type=\""+this.getClass().getSimpleName()+"\"");
		{
			w.tag("player-id", m_playerID);
			w.tag("unit-id", m_unitID);
			w.tag("unit-type", m_unitType);
			w.tag("x", m_x);
			w.tag("y", m_y);
		}
		w.tag("/condition");
	}
	
	protected static Condition loadConditionFromXML(Element e) {
		BuildingCompleteCondition c = 
			new BuildingCompleteCondition(e.getChildText("unit-type"),
					Integer.parseInt(e.getChildText("x")), 
					Integer.parseInt(e.getChildText("y")), 
					Integer.parseInt(e.getChildText("player-id")));
		c.setUnitType(e.getChildText("unit-type"));
		return c;
	}
	
}
